<template>
    <block v-if="scene == 1154">
    	<pyq-open-chunk></pyq-open-chunk>
    </block>
    <block v-else>
        <view class="page bg bg-fff">
            <up-gap :height="40"></up-gap>
            
            <view class="">
                <text>需要缴纳的金额为：</text>
                <text class="col-theme fontBold font20">{{ taxAmount }}</text>元
            </view>

            <view class="font15 fontBold top-20">所填信息</view>
            <view class="item flex">
                <text class="itamLabel">户籍情况:</text>
                <text class="moneyBox">{{ _familyStatus === 'shanghai' ? '沪籍' : '非沪籍' }}</text>
            </view>
            <view class="item flex">
                <text class="itamLabel">是否为首套:</text>
                <text class="moneyBox">{{_isFirst ? '是' : '否' }}</text>
            </view>
            <view class="item flex" v-if="_familyStatus === 'non-shanghai'">
                <text class="itamLabel">居住证/积分:</text>
                <text class="moneyBox">{{_residencePermit === 'full' ? '居住证满三年或积分120以上' : '居住证不满三年和积分不足120' }}</text>
            </view>
            <view class="item flex">
                <text class="itamLabel">家庭成员数:</text>
                <text class="moneyBox">{{_familyMember}}人</text>
            </view>
            <view class="item flex">
                <text class="itamLabel">新购住房面积:</text>
                <text class="moneyBox">{{_newHouseArea}}m²</text>
            </view>
            <view class="item flex">
                <text class="itamLabel">新购住房单价:</text>
                <text class="moneyBox">{{_newHousePrice}}元/m²</text>
            </view>
            <view class="item flex">
                <text class="itamLabel">已有房产总面积:</text>
                <text class="moneyBox">{{_hasHouseArea}}m²</text>
            </view>
            
            <view class="submitBtn bg-theme col-fff font15" @click="againCalc">重新计算</view>
            <view class="tips font12">
                注：
                <view class="top-5">1.本计算结果仅供参考，请以实际缴纳金额为准。</view>
                <!-- <block v-if="houseIndex == 1">
                    <view class="top-5">1.住房建筑面积在90平方米（含）以下的，贷款首付款比例不得低于20%；</view>
                    <view class="top-5">2.住房建筑面积在90平方米以上的，贷款首付款比例不得低于30%；</view>
                </block>
                <block v-else>
                    <view class="top-5">1.购买第二套改善型住房的，贷款首付款比例不得低于35%；</view>
                    <view class="top-5">2.购买第二套改善型住房在自贸区临港新片区以及嘉定、青浦、松江、奉贤、宝山、金山6个行政区的，贷款首付款比例不得低于30%；</view>
                </block>
                <view class="top-5">3.购买二星级及以上新建绿色建筑住房公积金(含补充)最高贷款额度上浮15%。</view>
                <view class="top-5">4.本计算结果仅供参考。</view> -->
            </view>
        </view>
    </block>
</template>

<script>
    import { mapState } from 'pinia';
    import { useUserInfoStore, useConfigStore } from '@/store/index';
    import { isBoolean } from '@/common/common';
    export default {
        data() {
            return {
                _familyStatus: '',
                _isFirst: '',
                _residencePermit: '',
                _familyMember: '',
                _newHouseArea: '',
                _newHousePrice: '',
                _hasHouseArea: '',
            }
        },
        computed:{
            ...mapState(useConfigStore, ['scene']),
            ...mapState(useUserInfoStore, ['userInfo'])
        },
        onShareAppMessage(res) {
            return {
                title: `公积金贷款额度计算器，点击查看详情`,
                path: `/pages/menus/tools/calculator/accumulate-limit-query?puid=${this.userInfo.id}&share=true`,
            	//#ifdef MP-TOUTIAO
            	templateId: '3e9tvurff9vob6ot7u',
            	//#endif
            }
        },
        
        //#ifdef MP-WEIXIN
        onShareTimeline() {
            return {
                title: `公积金贷款额度计算器，点击查看详情`,
                imageUrl: 'https://shoss.ipangsell.com/xcx/xcx-share-image.jpg',
                query: `puid=${this.userInfo.id}&share=true`
            }
        },
        //#endif
        
        onLoad(options) {
            const that = this;
            //朋友圈进入小程序快照场景
            if(this.scene == 1154) return;
            
            let decodeParamsObj = JSON.parse(decodeURIComponent(options.paramsObj));
            console.log(decodeParamsObj);
            this._familyStatus = decodeParamsObj.familyStatus;
            this._isFirst = decodeParamsObj.isFirst;
            this._residencePermit = decodeParamsObj.residencePermit;
            this._familyMember = decodeParamsObj.familyMember;
            this._newHouseArea = decodeParamsObj.newHouseArea;
            this._newHousePrice = decodeParamsObj.newHousePrice;
            this._hasHouseArea = decodeParamsObj.hasHouseArea;
            uni.$check_and_login(function () {
                that.initPage();
            });
        },
        methods:{
            //初始化
            initPage() {
                //写一套房产税算法：
                // 1.沪籍，首套，免征；
                // 2.沪籍，二套及以上，征收本年度应缴房产税；
                // 3.非沪籍，首套，居住证满三年或积分120以上，免征；
                // 4.非沪籍，首套，居住证不满三年和积分不足120，征收本年度应缴房产税；
                // 5.非沪籍，二套及以上，征收本年度应缴房产税；
                // 应税面积=家庭住房总面积-免税面积×家庭成员数
                // 税率=新购房单价在94466元以下的税率为0.4%，超过94466元的税率为0.6%；
                // 若人均建筑面积超过60平方米，超出部分将被视为应税面积，需按相应税率缴纳房产税。
                // 计算公式为：本年度应缴房产税 = 新购住房应税面积× 新购房单价 × 70% × 税率。
                // 假设页面通过options传递了以下参数：
                // familyStatus: 'shanghai' | 'non-shanghai'
                // isFirst: true | false  // 1:首套, 2:二套及以上
                // residencePermit: 'full' | 'notFull' // 非沪籍时有效
                // familyMember: 家庭成员数
                // newHouseArea: 新购住房面积(平方米)
                // newHousePrice: 新购房单价(元/平方米)
                // hasHouseArea: 家庭住房总面积(平方米)

                // 免税面积标准
                const EXEMPT_AREA_PER_PERSON = 60; // 每人60平米免税

                // 价格分界线
                const PRICE_THRESHOLD = 94466;

                // 税率
                const TAX_RATE_LOW = 0.004; // 0.4%
                const TAX_RATE_HIGH = 0.006; // 0.6%

                // 取参数
                const familyStatus = this._familyStatus || '';
                const isFirst = this._isFirst;
                const residencePermit = this._residencePermit || '';
                const familyMember = Number(this._familyMember || 1);
                const newHouseArea = Number(this._newHouseArea || 0);
                const newHousePrice = Number(this._newHousePrice || 0);
                const hasHouseArea = Number(this._hasHouseArea || 0);
                const totalHouseArea = hasHouseArea + newHouseArea;

                // 1. 判断是否免征
                let isTaxFree = false;
                if (familyStatus === 'shanghai') {
                    if (isFirst) {
                        // 沪籍首套免征
                        isTaxFree = true;
                    }
                } else if (familyStatus === 'non-shanghai') {
                    if (isFirst) {
                        // 非沪籍首套，居住证满三年或积分120以上，免征
                        if (residencePermit === 'full') {
                            // 非沪籍首套，居住证满三年或积分120以上，免征
                            isTaxFree = true;
                        }
                    }
                }

                // 2. 计算应税面积
                let exemptArea = EXEMPT_AREA_PER_PERSON * familyMember;
                let taxableArea = totalHouseArea - exemptArea;
                if (taxableArea < 0) taxableArea = 0;
                // 新购住房的应税面积不能超过新购住房面积
                let newHouseTaxableArea = Math.min(newHouseArea, taxableArea);

                // 3. 计算税率
                let taxRate = newHousePrice <= PRICE_THRESHOLD ? TAX_RATE_LOW : TAX_RATE_HIGH;

                // 4. 计算应缴房产税
                let taxAmount = 0;
                if (!isTaxFree && newHouseTaxableArea > 0 && newHousePrice > 0) {
                    taxAmount = newHouseTaxableArea * newHousePrice * 0.7 * taxRate;
                }

                // 5. 保存结果
                this.isTaxFree = isTaxFree;
                this.taxAmount = taxAmount;
                this.newHouseTaxableArea = newHouseTaxableArea;
                this.taxRate = taxRate;
                this.familyStatus = familyStatus;
                this.isFirst = isFirst;
                this.residencePermit = residencePermit;
                this.familyMember = familyMember;
                this.newHouseArea = newHouseArea;
                this.newHousePrice = newHousePrice;
                this.hasHouseArea = hasHouseArea;
                console.log(taxAmount)
            },

            //back
            againCalc(){
                uni.navigateBack({
                    delta: 1
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    .page{
        padding: 0 30rpx;
        min-height: 100vh;
        overflow: hidden;
        .item{
            padding: 20rpx 0rpx;
            justify-content: space-between;
            border-bottom: 1rpx solid rgba(0,0,0,.05);
            .itamLabel{
                color: #999;
            }
        }
        .submitBtn{
            height: 80rpx;
            line-height: 80rpx;
            border-radius: 10rpx;
            margin-top: 70rpx;
            text-align: center;
        }
        .tips{
            margin-top: 40rpx;
            line-height: 1.5;
        }
    }
</style>